<template>
    <div class="goods-buy-box">
        <div
            class="ctr-btn"
            @click="goOnlineService"
        >
            <img
                class="ctr-icon"
                src="../../../assets/images/goodsDetail/service-icon.png"
            />
            <div class="ctr-name">客服</div>
        </div>
        <router-link
            tag="div"
            to="/cart"
            class="ctr-btn"
        >
            <img
                class="ctr-icon"
                src="../../../assets/images/goodsDetail/cart-icon.png"
            />
            <div class="ctr-name">购物车</div>
        </router-link>
        <div
            class="free-get-btn noStock"
            v-if="stock==0||(goodsInfo.price_1==0&&goodsInfo.price_0==0)"
            @click="freeGet"
        >
            暂时缺货
        </div>
        <div
            class="first-get-btn"
            v-if="goodsType == 0&&stock>0&&goodsInfo.price_1>0&&(userInfo.vip_level==0||(userInfo.vip_level==1&&userData.free_times>0))"
            @click="firstGet"
        >
            首次白领
        </div>
        <div
            class="free-get-btn"
            v-if="goodsType == 0&&stock>0&&goodsInfo.price_1>0&&(userInfo.vip_level>1||(userInfo.vip_level==1&&userData.free_times==0))"
            @click="freeGet"
        >
            店主白领
        </div>
        <div
            class="member-buy-btn"
            v-if="goodsType == 0&&stock>0&&goodsInfo.price_0>0&&(userInfo.vip_level>1||(userInfo.vip_level==1&&userData.free_times==0))"
            @click="memberBuy"
        >
            会员价购买
        </div>
        <div
            class="be-vip-btn"
            @click="goBuy"
            v-if="stock>0&&goodsType == 1&&userInfo.vip_level<2"
        >
            立即成为店主
        </div>
        <div
            class="be-vip-btn"
            @click="goBuy"
            v-if="stock>0&&goodsType == 1&&userInfo.vip_level>1"
        >
            店主专享
        </div>
    </div>
</template>

<script>
export default {
    name: 'GoodsBuy',
    props: [
        'goodsType',
        'goodsId',
        'goodsNum',
        'userInfo',
        'userData',
        'stock',
        'goodsInfo'
    ],
    data() {
        return {
            isLoad: false
        }
    },
    mounted() {
        this.getEasemobimConfig()
    },
    methods: {
        getEasemobimConfig() {
            window.easemobim = window.easemobim || {}
            easemobim.config = {
                hide: true,
                autoConnect: true,
                hideKeyboard: true,
                visitor: {
                    trueName: this.userInfo.nick_name,
                    phone: this.userInfo.mobile_phone,
                    userNickname: this.userInfo.id
                }
            }
        },
        firstGet() {
            if (this.stock == 0) {
                return
            }
            if (this.userInfo.parent_id == 0) {
                this.$router.push('/inviteCode')
                return
            }
            if (this.userInfo.vip_level == 0) {
                this.$emit('beMember')
                return
            }
            if (this.isLoad) {
                return
            }
            let postData = this.qs.stringify({
                goods_id: this.goodsId,
                goods_num: this.goodsNum,
                price_type: 1
            })
            this.axios.post('/api/cart/add.json', postData).then(res => {
                this.isLoad = true
                const data = res.data
                if (data.status == 1) {
                    this.isLoad = false
                    this.$router.push('/cart')
                } else {
                    this.commonDialog.alert('提示', data.msg)
                    this.isLoad = false
                }
            })
        },
        freeGet() {
            if (this.stock == 0) {
                return
            }
            if (this.userInfo.parent_id == 0) {
                this.$router.push('/inviteCode')
                return
            }
            if (this.userInfo.vip_level == 0) {
                this.$emit('beMember')
                return
            }
            if (
                this.userInfo.mobile_phone == null ||
                this.userInfo.mobile_phone == ''
            ) {
                this.$emit('goBindPhone')
                return
            }
            if (this.isLoad) {
                return
            }
            let postData = this.qs.stringify({
                goods_id: this.goodsId,
                goods_num: this.goodsNum,
                price_type: 1
            })
            this.axios.post('/api/cart/add.json', postData).then(res => {
                this.isLoad = true
                const data = res.data
                if (data.status == 1) {
                    this.isLoad = false
                    this.$router.push('/cart')
                } else {
                    this.commonDialog.alert('提示', data.msg)
                    this.isLoad = false
                }
            })
        },
        memberBuy() {
            if (this.stock == 0) {
                return
            }
            if (this.userInfo.parent_id == 0) {
                this.$router.push('/inviteCode')
                return
            }
            if (this.userInfo.vip_level == 0) {
                this.$emit('beMember')
                return
            }
            if (
                this.userInfo.mobile_phone == null ||
                this.userInfo.mobile_phone == ''
            ) {
                this.$emit('goBindPhone')
                return
            }
            if (this.isLoad) {
                return
            }
            let postData = this.qs.stringify({
                goods_id: this.goodsId,
                goods_num: this.goodsNum,
                price_type: 0
            })
            this.axios.post('/api/cart/add.json', postData).then(res => {
                this.isLoad = true
                const data = res.data
                if (data.status == 1) {
                    this.isLoad = false
                    this.$router.push('/cart')
                } else {
                    this.commonDialog.alert('提示', data.msg)
                    this.isLoad = false
                }
            })
        },
        goOnlineService() {
            easemobim.bind({ configId: 'c490fa9c-6496-48ea-992a-667f779852ae' })
        },
        goBuy() {
            if (this.userInfo.parent_id == 0) {
                this.$router.push('/inviteCode')
                return
            }
            if (
                this.userInfo.mobile_phone == null ||
                this.userInfo.mobile_phone == ''
            ) {
                this.$emit('goBindPhone')
                return
            }
            this.$emit('beVip')
        }
    }
}
</script>

<style lang="stylus" scoped>
.goods-buy-box
    position absolute
    bottom 0
    left 0
    right 0
    height 1.31rem
    background #fff
    box-sizing border-box
    display flex
    .ctr-btn
        width 14%
        height 100%
        text-align center
        padding-top 0.27rem
        box-sizing border-box
        border-right 1px solid #e5e5e5
        border-top 1px solid #e5e5e5
        .ctr-icon
            width 0.43rem
            height 0.43rem
        .ctr-name
            font-size 0.27rem
            color #969696
            margin-top 0.11rem
    .add-cart-btn
        flex 1
        line-height 1.31rem
        text-align center
        font-size 0.38rem
        color #666
    .first-get-btn
        flex 1
        line-height 1.31rem
        text-align center
        font-size 0.38rem
        color #fff
        background #ee5a24
    .free-get-btn
        flex 1
        line-height 1.31rem
        text-align center
        font-size 0.38rem
        color #fff
        background #ee5a24
    .member-buy-btn
        flex 1
        line-height 1.31rem
        text-align center
        font-size 0.38rem
        color #fff
        background #d7463c
    .be-vip-btn
        flex 1
        line-height 1.31rem
        text-align center
        font-size 0.38rem
        color #fff
        background #d7463c
    .go-experience
        flex 1
        line-height 1.31rem
        text-align center
        font-size 0.38rem
        color #fff
        background #d7463c
    .no-times
        background #ededed
    .noStock
        background #ffca00
</style>

